home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: How do I round and truncate floats to integers?
- Date: 21 Feb 1996 01:14:00 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb20181400@qcd.lanl.gov>
- References: <4g009b$c2n@news.tuwien.ac.at> <harmon.824447677@pegasus.montclair.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: harmon@pegasus.montclair.edu's message of 16 Feb 1996 00:38:09 -0500
-
- In article <harmon.824447677@pegasus.montclair.edu>
- harmon@pegasus.montclair.edu (Derek Harmon) writes:
- <snip>
- > If typecasting is supposed to do the trick, then how do I
- >distinguish cases 1) and 2)?
-
- It is important to note that a C typecast only tells the compiler to treat
- some form of data as another form of data. No change is made to the data.
- The binary representation of a double floating point value is very different
- from that of an integer, and just telling the compiler to treat it as an int
- will not solve your problem (for example, 7 != (int)'7').
-
- If you are not proficient in C, please test your statements before
- posting answers. Nobody has the right to air their confusion in a
- forum which is likely to mislead other people. A cast in C can indeed
- change the bit pattern that represents a value.
-
- In particular, find a compiler that gives (int)7.5 != 7 or (int)7.4 !=
- 7. Do you think that the bit representation of the double 7.5 is the
- same as that of the int 7? Or do you think that 7.5 and 7.4 have the
- same bit string?
-
- (int)'7' is a bit ridiculous. '7' is an int, converting it to int just
- gives back the same int viz. '7'. The difference here is that the int
- has been represented as a character literal, and the character literal
- '7' is different from the decimal constant 7. It is like saying that
- the octal constant 023 is not the same as the decimal constant 23: it
- certainly isn't: in fact (int)023 != 23, but that has little to do
- with the cast.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-